package actions
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import interfaces.IAction;

	public class PropsActionGroup extends EventDispatcher implements IAction
	{
		public static const SERIAL:int = 0;		// 按顺序串行
		public static const NOSERIAL:int = 1;		// 并行
		private var m_pas:Vector.<PropsAction>;
		private var m_type:int;
		private var m_start:Boolean;
		private var m_pause:Boolean;
		private var m_index:int;
		
		public function PropsActionGroup()
		{
			m_pas = new Vector.<PropsAction>();
			m_type = NOSERIAL;
		}
		
		public function set start(b:Boolean):void
		{
			m_start = b;
			if(!b)
			{
				dispatchEvent(new Event(Event.COMPLETE));
			}else
			{
				for(var i:int = 0; i< m_pas.length; i++)
				{
					m_pas[i].start = true;
				}
				m_index = 0;
			}
		}
		
		public function get start():Boolean
		{
			return m_start;
		}
		
		public function set pause(b:Boolean):void
		{
			m_pause = b;
			for(var i:int = 0; i< m_pas.length; i++)
			{
				m_pas[i].pause = b;
			}
		}
		
		public function get pause():Boolean
		{
			return m_pause;
		}
		
		public function update(e:Object = null):void
		{
			if(!m_start)return;
			if(m_pas.length <= 0)
			{
				start = false;
				return;
			}
			if(m_type == PropsActionGroup.NOSERIAL)
			{
				updateNoserial();
			}
			if(m_type == PropsActionGroup.SERIAL)
			{
				updateSerial();
			}
		}
		
		public function set list(pas:Vector.<PropsAction>):void
		{
			m_pas =pas;
		}
		
		public function get list():Vector.<PropsAction>
		{
			return m_pas;
		}
		
		public function set type(n:int):void
		{
			m_type = n;
		}
		
		public function get type():int
		{
			return m_type;
		}
		
		public function get index():int
		{
			return m_index;
		}
		
		private function updateNoserial():void
		{
			for(var i:int = 0; i < m_pas.length; i++)
			{
				if(m_pas[i].start)m_pas[i].update();
				else 
				{
					m_pas.splice(i,1);
					i --;
				}
			}
		}
		
		private function updateSerial():void
		{
			for(var i:int = 0; i < m_pas.length; i++)
			{
				if(m_pas[i].start)
				{
					m_pas[i].update();
					break;
				}
				else 
				{
					m_pas.splice(i,1);
					i --;
					m_index ++;
					dispatchEvent(new Event(Event.CHANGE));
				}
			}
		}
	}
}